package com.wan37.logic.email.dao.impl;

import com.wan37.logic.email.dao.AttachmentDao;
import com.wan37.logic.email.domain.entity.AttachmentDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-07-28 11:02
 **/
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
@Repository
public class AttachmentDaoImpl implements AttachmentDao {
    @Autowired
    private HibernateTemplate hibernateTemplate;
    @Override
    public boolean add(AttachmentDO attachmentDO) {
        try {
            hibernateTemplate.save(attachmentDO);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean delete(Integer id) {
        try {
            hibernateTemplate.delete(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean update(AttachmentDO attachmentDO) {
        try{
            hibernateTemplate.update(attachmentDO);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public List<AttachmentDO> getAll() {
        return (List<AttachmentDO>) hibernateTemplate.find("from AttachmentDO ");
    }

    @Override
    public List<AttachmentDO> getAttachmentDOByEmailId(String emailId) {

        return (List<AttachmentDO>) hibernateTemplate.find("from AttachmentDO where emailId  = ?", emailId);
    }
}
